package control;

import java.io.IOException;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import swimv2.ejb.remote.GestioneCapacitaRemote;
import swimv2.ejb.util.LoginToken;
import swimv2.ejb.entity.Capacita;
import util.HtmlUtil;

/**
 * Servlet implementation class Profilo
 */
public class Profilo extends Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see Servlet#Servlet()
     */
    public Profilo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Capacita> capacitaConosciute;
		List<Capacita> capacitaSconosciute;
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
	    HtmlUtil.inviaHeader(request, response);
	    HtmlUtil.inviaIntestazioneUtente(request, response);
		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			
			
			String username=tok.getUsername();
			
			capacitaConosciute=gestioneCapacitaRemote.elencoCapacitaUtente(username);
			capacitaSconosciute=gestioneCapacitaRemote.elencoNonCapacitaUtente(username);
			request.setAttribute("capacita", capacitaConosciute);
			request.setAttribute("incapacita",capacitaSconosciute);
		    HtmlUtil.includi(request, response, "/WEB-INF/include/CapacitaProfilo.jsp");

		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    HtmlUtil.inviaFooter(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String azione=(String) request.getParameter("azione");
		if(azione.equals("aggiungiCapacita"))
		{
			aggiungiCapacita(request,response);
		}
		if(azione.equals("rimuoviCapacita"))
		{
			rimuoviCapacita(request,response);
		}
	}

	private void aggiungiCapacita(HttpServletRequest request,HttpServletResponse response) {
		String[] lista = request.getParameterValues("CapacitaAggiunte");
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");

		String username=tok.getUsername();
		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
		
			for(String cap:lista)
			{
				int id = Integer.parseInt(cap);
			    gestioneCapacitaRemote.dichiaraCapacita(username, id);		
			}
		    HtmlUtil.redirect(request, response, "/Profilo");

		
		}
		catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
	}
	
	private void rimuoviCapacita(HttpServletRequest request,HttpServletResponse response) {
		String[] lista = request.getParameterValues("CapacitaRimosse");
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();

		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
		
			for(String cap:lista)
			{
				int id = Integer.parseInt(cap);
			    gestioneCapacitaRemote.dichiaraNonCapacita(username, id);

			}
		    HtmlUtil.redirect(request, response, "/Profilo");

		
		}
		catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
	}

}
